package com.uni.domain;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;

import java.io.Serializable;
import java.sql.*;
import java.util.Arrays;

public class PhotoType implements UserType {

    public int[] sqlTypes() {
        return new int[]{Types.BLOB};
    }

    public Class returnedClass() {
        return byte[].class;
    }

    public boolean equals(Object o, Object o1) throws HibernateException {
        return Arrays.equals((byte[]) o, (byte[]) o1);
    }

    public int hashCode(Object o) throws HibernateException {
        return o.hashCode();
    }

    public Object nullSafeGet(ResultSet resultSet, String[] strings, Object o) throws HibernateException, SQLException {
        Blob blob = resultSet.getBlob(strings[0]);
        return blob.getBytes(1, (int) blob.length());
    }

    public void nullSafeSet(PreparedStatement st, Object value, int index)
            throws HibernateException, SQLException {
        st.setBlob(index, Hibernate.createBlob((byte[]) value));
    }

    public Object deepCopy(Object value) {
        if (value == null) return null;

        byte[] bytes = (byte[]) value;
        byte[] result = new byte[bytes.length];
        System.arraycopy(bytes, 0, result, 0, bytes.length);

        return result;
    }

    public boolean isMutable() {
        return true;
    }

    public Serializable disassemble(Object o) throws HibernateException {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public Object assemble(Serializable serializable, Object o) throws HibernateException {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public Object replace(Object o, Object o1, Object o2) throws HibernateException {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

}
